package com.jhgsys.internal.incentive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.incentive.dto.IncentiveRewardMattersDto;
import com.jhgsys.internal.incentive.entity.IncentiveRewardMatters;
import com.jhgsys.internal.incentive.mapper.IncentiveRewardMattersMapper;
import com.jhgsys.internal.incentive.service.IIncentiveRewardMattersService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.system.entity.Tenant;
import com.jhgsys.internal.system.service.ITenantService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目设置 Service实现
 *
 * @author admin
 * @date 2024-07-29 14:19:15
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveRewardMattersServiceImpl extends ServiceImpl<IncentiveRewardMattersMapper, IncentiveRewardMatters> implements IIncentiveRewardMattersService {

    @Autowired
    private ITenantService tenantService;


    /**
     * 查询（分页）
     * @author admin
     * @date 2024-07-29 14:19:15
     * @param request QueryRequest
     * @param incentiveRewardMatters IncentiveRewardMatters
     * @return IPage<BaseProject>
     */
    @Override
    public IPage<IncentiveRewardMattersDto> findIncentiveRewardMatters(QueryRequest request, IncentiveRewardMatters incentiveRewardMatters) {
        LambdaQueryWrapper<IncentiveRewardMatters> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardMatters::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(incentiveRewardMatters.getMattersName())){
            queryWrapper.like(IncentiveRewardMatters::getMattersName,incentiveRewardMatters.getMattersName());
        }
        if(StringUtils.isNotBlank(incentiveRewardMatters.getTenantId())){
            queryWrapper.eq(IncentiveRewardMatters::getTenantId, incentiveRewardMatters.getTenantId());
        }
        if(StringUtils.isNotBlank(incentiveRewardMatters.getStatus())){
            queryWrapper.eq(IncentiveRewardMatters::getStatus, incentiveRewardMatters.getStatus());
        }
        queryWrapper.orderByAsc(IncentiveRewardMatters::getSort);
        Page<IncentiveRewardMatters> page = new Page<>(request.getPageNum(), request.getPageSize());

        IPage<IncentiveRewardMatters> result = this.page(page, queryWrapper);
        IPage<IncentiveRewardMattersDto> resultPage= new Page<>();

        List<IncentiveRewardMatters> records = result.getRecords();
        List<IncentiveRewardMattersDto> baseIncentiveRewardMattersDtoDtoList = BeanHelper.copyWithCollection(records,IncentiveRewardMattersDto.class);
        resultPage.setRecords(baseIncentiveRewardMattersDtoDtoList);
        resultPage.setTotal(result.getTotal());
        return resultPage;
    }

    /**
     * 根据ID查询
     * @author admin
     * @date 2024-07-29 14:19:15
     * @param id
     * @return BaseProject
     */
    @Override
    public IncentiveRewardMattersDto findIncentiveRewardMattersyId(String id) {
        return null;
    }

    /**
     * 新增
     * @author Lujiachen
     * @date 2024-08-09 14:19:15
     * @param incentiveRewardMatters
     */
    @Override
    public void createIncentiveRewardMatters(IncentiveRewardMatters incentiveRewardMatters) {
        incentiveRewardMatters.setCreatedTime(new Date());
        this.save(incentiveRewardMatters);
    }

    /**
     * 修改
     * @author Lujiachen
     * @date 2024-08-09 15:12:12
     * @param incentiveRewardMatters
     */
    @Override
    public void updateBaseArea(IncentiveRewardMatters incentiveRewardMatters) {
        incentiveRewardMatters.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveRewardMatters);
    }
    /**
     * 删除
     * @author Lujiachen
     * @date 2024-08-09 15:12:12
     * @param rewardMattersId
     */
    @Override
    public void deleteIncentiveRewardMatters(IncentiveRewardMatters incentiveRewardMatters, String rewardMattersId) {
        List<String> list = Arrays.asList(rewardMattersId.split(StringPool.COMMA));
        this.baseMapper.update(incentiveRewardMatters,new LambdaQueryWrapper<IncentiveRewardMatters>().in(IncentiveRewardMatters::getRewardMattersId,list));
    }
}
